package com.pk.platform.business.core.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.core.dao.IFormDao;
import com.pk.platform.domain.form.FormDefine;
import com.pk.platform.domain.form.FormModule;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.page.Pager;

public class FormDaoImpl extends GenericDaoImpl implements IFormDao {

	@Override
	public int defineCountByCode(String code, String id) {
		String sql = "select count(id) from p_formdefine where formCode = ?";
		List<Object> param = new ArrayList<Object>();
		param.add(code);
		if(StringConverter.isNotNull(id))
		{
			sql += " and id <> ?";
			param.add(id);
		}
		return this.getSjt().queryForInt(sql, param.toArray());
	}

	@Override
	public int moduleCountByCode(String code, String id) {
		String sql = "select count(id) from p_formmodule where moduleCode = ?";
		List<Object> param = new ArrayList<Object>();
		param.add(code);
		if(StringConverter.isNotNull(id))
		{
			sql += " and id <> ?";
			param.add(id);
		}
		return this.getSjt().queryForInt(sql, param.toArray());
	}

	@Override
	public FormDefine queryFormDefineByCode(String code) {
		String hql = "from FormDefine fd where fd.formCode = ?";
		List<FormDefine> list = this.getHibernateTemplate().find(hql, code);
		if(list.size() > 0)
			return list.get(0);
		return null;
	}

	@Override
	public List<Map<String, Object>> queryFormDefineByModule(String fmId,
			Pager pager) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from p_formdefine where fmId = ?");
		return this.getListByPage(sql.toString(), new Object[]{fmId}, pager);
	}

	@Override
	public List<Map<String, Object>> queryFormModule(Pager pager) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from p_formmodule");
		return this.getListByPage(sql.toString(), null, pager);
	}

	@Override
	public FormModule queryFormModuleByCode(String code) {
		String hql = "from FormModule fm where fm.moduleCode = ?";
		List<FormModule> list = this.getHibernateTemplate().find(hql, code);
		if(list.size() > 0)
			return list.get(0);
		return null;
	}

}
